// pkg/util/user_agent.go
package util

import (
	"github.com/gin-gonic/gin"
	"strings"
)

// GetBrowser 获取浏览器信息
func GetBrowser(c *gin.Context) string {
	userAgent := c.Request.UserAgent()

	// 检查常见浏览器
	if strings.Contains(userAgent, "Edge") || strings.Contains(userAgent, "Edg") {
		return "Microsoft Edge"
	} else if strings.Contains(userAgent, "Firefox") {
		return "Mozilla Firefox"
	} else if strings.Contains(userAgent, "Chrome") && !strings.Contains(userAgent, "Chromium") {
		return "Google Chrome"
	} else if strings.Contains(userAgent, "Safari") && !strings.Contains(userAgent, "Chrome") && !strings.Contains(userAgent, "Chromium") {
		return "Apple Safari"
	} else if strings.Contains(userAgent, "Opera") || strings.Contains(userAgent, "OPR") {
		return "Opera"
	} else if strings.Contains(userAgent, "MSIE") || strings.Contains(userAgent, "Trident") {
		return "Internet Explorer"
	}

	// 未识别的浏览器
	return "未知浏览器"
}

// GetOs 获取操作系统信息
func GetOs(c *gin.Context) string {
	userAgent := c.Request.UserAgent()

	// 检查常见操作系统
	if strings.Contains(userAgent, "Windows") {
		if strings.Contains(userAgent, "Windows NT 10.0") {
			return "Windows 10"
		} else if strings.Contains(userAgent, "Windows NT 6.3") {
			return "Windows 8.1"
		} else if strings.Contains(userAgent, "Windows NT 6.2") {
			return "Windows 8"
		} else if strings.Contains(userAgent, "Windows NT 6.1") {
			return "Windows 7"
		} else {
			return "Windows"
		}
	} else if strings.Contains(userAgent, "Macintosh") || strings.Contains(userAgent, "Mac OS X") {
		return "Mac OS"
	} else if strings.Contains(userAgent, "Linux") {
		if strings.Contains(userAgent, "Android") {
			return "Android"
		} else {
			return "Linux"
		}
	} else if strings.Contains(userAgent, "iPhone") || strings.Contains(userAgent, "iPad") || strings.Contains(userAgent, "iPod") {
		return "iOS"
	}

	// 未识别的操作系统
	return "未知操作系统"
}
